package com.pan.admin.module.visualizer.entity.vo;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Empty;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.constant.ProductApplyFlowStatusEnum;
import com.pan.admin.module.visualizer.entity.po.LoanProductVisualizerPO;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 *  主体业务数据可视化VO实体
 *  @author 周硕
 */
@Data
public class LoanProductVisualizerVO {

    /** 主体名称 */
    private String loanProductName;
    /** 主体Id */
    private String loanProductId;
    /** 主体撞库成功总条数 */
    private Integer loanProductDataExchangeSucTotal = 0;
    /** 主体撞库失败总条数 */
    private Integer loanProductDataExchangeFailTotal = 0;
    /** 主体进件成功总条数 */
    private Integer loanProductApplyBizSucTotal = 0;
    /** 主体 机构-进件时业务失败总条数 */
    private Integer loanProductApplyBizFailTotal = 0;
    /** 截止时间 */
    private String dateTime;

    /**
     *  转换VO
     *
     *  @param  list
     *  @return List
     */
    public static List<LoanProductVisualizerVO> convert(List<LoanProductVisualizerPO> list) {
        if (ValidParam.isEmpty(list)) {
            return Empty.list();
        }

        List<LoanProductVisualizerVO> listVO = CollUtils.newArrayList();
        Map<Integer, List<LoanProductVisualizerPO>> loanProductMap = CollUtils.groupBy(list, t -> t.getLoanProductId());
        IteratorUtils.entrySet(loanProductMap, (key, listloanProduct) -> {
            LoanProductVisualizerVO loanProductVisualizerVO = new LoanProductVisualizerVO();
            LoanProductVisualizerPO first = IteratorUtils.getFirst(listloanProduct);
            loanProductVisualizerVO.setLoanProductName(first.getLoanProductName());
            loanProductVisualizerVO.setLoanProductId(String.valueOf(first.getLoanProductId()));
            //  状态分组
            Map<Integer, List<LoanProductVisualizerPO>> statusMap = CollUtils.groupBy(listloanProduct, t -> t.getStatus());
            //  机构撞库成功 21
            LoanProductVisualizerPO loanProductDataChangeSuc = getByStatus(statusMap, ProductApplyFlowStatusEnum.DATA_EXCHANGE_SUC);
            if (ValidParam.isNotNull(loanProductDataChangeSuc)) {
                loanProductVisualizerVO.setLoanProductDataExchangeSucTotal(loanProductDataChangeSuc.getTotal());
            }
            //  机构撞库失败 20
            LoanProductVisualizerPO loanProductDataChangeFail = getByStatus(statusMap, ProductApplyFlowStatusEnum.DATA_EXCHANGE_FAIL);
            if (ValidParam.isNotNull(loanProductDataChangeFail)) {
                loanProductVisualizerVO.setLoanProductDataExchangeFailTotal(loanProductDataChangeFail.getTotal());
            }
            //   撞库时业务失败 24
            LoanProductVisualizerPO loanProductDataChangeFail24 = getByStatus(statusMap, ProductApplyFlowStatusEnum.DATA_EXCHANGE_BIZ_FAIL);
            if (ValidParam.isNotNull(loanProductDataChangeFail24)) {
                if (ValidParam.isNotNull(loanProductVisualizerVO.getLoanProductDataExchangeFailTotal())) {
                    Integer count = loanProductVisualizerVO.getLoanProductDataExchangeFailTotal() + loanProductDataChangeFail24.getTotal();
                    loanProductVisualizerVO.setLoanProductDataExchangeFailTotal(count);
                }
            }
            //  撞库超时 25
            LoanProductVisualizerPO loanProductDataChangeFail25 = getByStatus(statusMap, ProductApplyFlowStatusEnum.DATA_EXCHANGE_REQ_TIMEOUT);
            if (ValidParam.isNotNull(loanProductDataChangeFail25)) {
                if (ValidParam.isNotNull(loanProductVisualizerVO.getLoanProductDataExchangeFailTotal())) {
                    Integer count = loanProductVisualizerVO.getLoanProductDataExchangeFailTotal() + loanProductDataChangeFail25.getTotal();
                    loanProductVisualizerVO.setLoanProductDataExchangeFailTotal(count);
                }
            }
            //  机构进件业务失败  23
            LoanProductVisualizerPO loanProductApplyBizFail = getByStatus(statusMap, ProductApplyFlowStatusEnum.APPLY_BIZ_ERROR);
            if (ValidParam.isNotNull(loanProductApplyBizFail)) {
                loanProductVisualizerVO.setLoanProductApplyBizFailTotal(loanProductApplyBizFail.getTotal());
            }
            //  机构进件成功  11
            LoanProductVisualizerPO loanProductApplyBizSuc = getByStatus(statusMap, ProductApplyFlowStatusEnum.USER_ACCEPT);
            if (ValidParam.isNotNull(loanProductApplyBizSuc)) {
                loanProductVisualizerVO.setLoanProductApplyBizSucTotal(loanProductApplyBizSuc.getTotal());
            }
            //  机构进件返回失败  22
            LoanProductVisualizerPO loanProductApplyFailTotal = getByStatus(statusMap, ProductApplyFlowStatusEnum.APPLY_ERROR);
            if (ValidParam.isNotNull(loanProductApplyFailTotal)) {
                if (ValidParam.isNotNull(loanProductVisualizerVO.getLoanProductApplyBizFailTotal())) {
                    Integer count = loanProductVisualizerVO.getLoanProductApplyBizFailTotal() + loanProductApplyFailTotal.getTotal();
                    loanProductVisualizerVO.setLoanProductApplyBizFailTotal(count);
                }
            }
            listVO.add(loanProductVisualizerVO);
        });
        return listVO;
    }
    /**
     *  根据状态获取对应数据
     *
     *  @param  statusMap
     *  @param  productApplyFlowStatusEnum
     *  @return loanProductVisualizerPO
     */
    private static LoanProductVisualizerPO getByStatus(Map<Integer, List<LoanProductVisualizerPO>> statusMap, ProductApplyFlowStatusEnum productApplyFlowStatusEnum) {
        List<LoanProductVisualizerPO> list = statusMap.get(productApplyFlowStatusEnum.getType());
        if (ValidParam.isNotEmpty(list)) {
            return IteratorUtils.getFirst(list);
        }
        return null;
    }
}
